from django.db import models
from user.models import User


class Client(models.Model):
    name = models.CharField("名称", max_length=100, unique=True)

    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f'{self.name}（客户）'

    class Meta:
        verbose_name = verbose_name_plural = "客户"


class ClientManager(models.Model):
    client = models.ForeignKey(Client, verbose_name="客户", on_delete=models.CASCADE)
    user = models.ForeignKey(User, verbose_name="客户经理", on_delete=models.CASCADE)

    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f'{self.client.name}（客户）-{self.user.name}（客户经理）'

    class Meta:
        unique_together = ('client', 'user')
        verbose_name = verbose_name_plural = "客户的客户经理"


class ClientVisit(models.Model):
    client = models.ForeignKey(Client, verbose_name="客户", on_delete=models.CASCADE)
    KIND_CHOICES = (
        (1, '日常拜访'),
        (2, '破冰拜访'),
    )
    kind = models.IntegerField("类别", default=1, choices=KIND_CHOICES)
    creater = models.CharField("派单人", max_length=100)
    receiver = models.CharField("收单人", max_length=100)
    formid = models.CharField("收集表id", max_length=100, null=True, blank=True)
    STATUS_CHOICES = (
        (0, '创建'),
        (1, '回单'),
        (2, '审批'),
        (3, '成功'),
        (4, '失败'),
    )
    status = models.IntegerField("状态", default=0, choices=STATUS_CHOICES)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f'{self.client.__str__()}的拜访记录'

    class Meta:
        verbose_name = verbose_name_plural = "客户拜访记录"


class ClientVisitReplyDaily(models.Model):
    client_visit = models.OneToOneField(ClientVisit, verbose_name="客户拜访", on_delete=models.CASCADE)
    replier = models.ForeignKey(User, verbose_name="回单人", on_delete=models.CASCADE)
    content = models.TextField("回单内容")
    location = models.TextField("回单位置")
    photos = models.TextField("回单照片")
    sp_no = models.CharField("审批单号", max_length=100, null=True, blank=True)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f'{self.client_visit.__str__()}的日常拜访回单'

    class Meta:
        verbose_name = verbose_name_plural = "日常拜访回单"


class ClientVisitReplyIce(models.Model):
    client_visit = models.OneToOneField(ClientVisit, verbose_name="客户拜访", on_delete=models.CASCADE)
    replier = models.ForeignKey(User, verbose_name="回单人", on_delete=models.CASCADE)
    content = models.TextField("回单内容")
    location = models.TextField("回单位置")
    photos = models.TextField("回单照片")
    sp_no = models.CharField("审批单号", max_length=100, null=True, blank=True)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f'{self.client_visit.__str__()}的破冰拜访回单'

    class Meta:
        verbose_name = verbose_name_plural = "破冰拜访回单"


class ClientVisitReplyDailyReview(models.Model):
    reply = models.OneToOneField(ClientVisitReplyDaily, verbose_name="拜访回单", on_delete=models.CASCADE)
    STATUS_CHOICES = (
        (1, '审批'),
        (2, '同意'),
        (3, '驳回'),
        (4, '转审'),
    )
    status = models.IntegerField("状态", default=0, choices=STATUS_CHOICES)
    approver = models.CharField("审批人userid", max_length=100, null=True, blank=True)
    speech = models.CharField("审批意见字段", max_length=100, null=True, blank=True)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f'{self.reply.__str__()}的审批'

    class Meta:
        verbose_name = verbose_name_plural = "日常拜访回单审批"


class ClientVisitReplyIceReview(models.Model):
    reply = models.OneToOneField(ClientVisitReplyIce, verbose_name="拜访回单", on_delete=models.CASCADE)
    STATUS_CHOICES = (
        (1, '审批'),
        (2, '同意'),
        (3, '驳回'),
        (4, '转审'),
    )
    status = models.IntegerField("状态", default=0, choices=STATUS_CHOICES)
    approver = models.CharField("审批人userid", max_length=100, null=True, blank=True)
    speech = models.CharField("审批意见字段", max_length=100, null=True, blank=True)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f'{self.reply.__str__()}的审批'

    class Meta:
        verbose_name = verbose_name_plural = "破冰拜访回单审批"
